using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net;
using System.Net.Cache;
using System.IO;
using System.Linq;
using System.IO.Compression;
using System.Threading;

namespace QiangPiaoTool
{
    public class HttpHelper
    {
        public static Stream GetStream(string method, string referer, string url, string data, CookieContainer cookieContainer)
        {

   
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => { return true; };
            }
          
            request.Method = "GET";
           
            request.KeepAlive = true;
            request.Accept = "*/*";
            request.Referer = referer;
            request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
            request.CookieContainer = cookieContainer;
            HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
            request.CachePolicy = policy;
            request.AllowWriteStreamBuffering = false;
            request.Headers.Add("Accept-Language: zh-cn");
            request.AllowAutoRedirect = false;
            //request.Headers.Add("Accept-Encoding: gzip, deflate");

            if ("POST".Equals(method.Trim().ToUpper()))
            {
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                byte[] byteRequest = Encoding.UTF8.GetBytes(data);
                request.ContentLength = byteRequest.Length;
                Stream stream = request.GetRequestStream();
                stream.Write(byteRequest, 0, byteRequest.Length);
                stream.Close();
            }

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
           
            Stream responseStream = response.GetResponseStream();
            if (response.ContentEncoding.ToLower().Contains("gzip"))
            {
                responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
            }
            if (cookieContainer != null)
            {
                CookieCollection ccl = cookieContainer.GetCookies(response.ResponseUri);
                if (ccl != null && ccl.Count > 0)
                {
                    foreach (Cookie ck in ccl)
                    {
                        ck.Expires = DateTime.Now.AddYears(1);
                    }
                }
            }
            return responseStream;
        }

        public static string GetString(string method, string referer, string url, string data, CookieContainer cookieContainer)
        {

            Stream responseStream = GetStream(method, referer, url, data, cookieContainer);
            StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
            string html = streamReader.ReadToEnd();
            responseStream.Close();
            streamReader.Close();
            return html;
        }



    }
}
